package com.example.asus.zyfypt_tea_307plx.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.asus.zyfypt_tea_307plx.R;
import com.example.asus.zyfypt_tea_307plx.activities.ViewArticleActivity;
import com.example.asus.zyfypt_tea_307plx.bean.ArticleBean;
import com.example.asus.zyfypt_tea_307plx.common.common;
import com.squareup.picasso.Picasso;

import java.util.List;

public class ArticleAdapter extends RecyclerView.Adapter {
    private List<ArticleBean> list;//向rv中填充的数据
    private Context context;//上下文
    private LayoutInflater layoutInflater;//动态布局
    OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(
                R.layout.item, parent, false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final ArticleBean bean = list.get(position);
        final int mPostion = position;
        if (bean == null)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        Picasso.with(context)
                .load(common.IMAGEURL + bean.getThumb())
                .placeholder(R.mipmap.ic_launcher)
                .into(viewHolder.imageView);
        viewHolder.tvtitle.setText(bean.getName());

        viewHolder.tvauthor.setText(bean.getAuthor());
        viewHolder.tvtime.setText(bean.getUpdate_time());

        if (mOnItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //取出当前item的id
                    int id=list.get(position).getId();
                    Toast.makeText(context, ""+id, Toast.LENGTH_SHORT).show();

                    Intent intent=new Intent(context, ViewArticleActivity.class);
                    intent.putExtra("resid",bean.getId());
                    context.startActivity(intent);

                }
            });

        }
    }

    @Override
    public int getItemCount() {
        if (list != null)
            return list.size();
        else
            return 0;

    }


    public ArticleAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    //自定义 ViewHolder子类，容纳item视图
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView tvtitle, tvauthor, tvtime;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            tvtitle = itemView.findViewById(R.id.textView);
            tvauthor = itemView.findViewById(R.id.textView2);
            tvtime = itemView.findViewById(R.id.textView3);
        }
    }

    //自定义 设置数据list
    public void setList(List<ArticleBean> list) {
        this.list = list;
        notifyDataSetChanged();//通知RV刷新数据
    }
}

